﻿using UnityEngine;
using System.Collections;
using System;

[LuaExport]
public class RootManager : MonoBehaviour
{
    public Action<int> OnLevelWasLoadedProxy;
    public static T GetManager<T>() where T : Component
    {
        return GameObject.Find("Root").GetComponentInChildren<T>();

    }
    public T AddManager<T>() where T : Component
    {

        GameObject obj = new GameObject(typeof(T).ToString());
        DontDestroyOnLoad(obj);
        obj.transform.parent = this.transform;
        Type t = typeof(T);
        return obj.AddComponent<T>();
    }
    // Use this for initialization
    void Start()
    {
        AddManager<LuaScriptManager>();
        AddManager<HandlerManager>();
        AddManager<NetManager>();
        AddManager<ResourceManager>();
    }

    // Update is called once per frame
    void Update()
    {


    }
    void OnLevelWasLoaded(int level)
    {
        if (OnLevelWasLoadedProxy != null)
            OnLevelWasLoadedProxy(level);
    }
    void OnDestory()
    {
        OnLevelWasLoadedProxy = null;
    }
}
